<?php


/**
 * This represents a general user form
 */

class UserForm extends CFormModel {

	const CREATE = 'create';
	const UPDATE = 'update';
	const CHANGE_PASSWORD = 'changePassword';

	public $username;
	public $password;
	public $email;
	public $language;
	public $first_name;
	public $last_name;
	public $phone_number;
	public $initials;
	public $simulation;
	public $password_check;
	private $user = null;
	private $roles = null;
	
	public function __construct() {
	}

	public function rules() {
		return array (
			array (
				'password, password_check',
				'required',
				'on' => self::CREATE . ',' . self::CHANGE_PASSWORD
			),
			array (
				'username, email, language, first_name',
				'required',
				'on' => self::UPDATE . ',' . self::CREATE
			),
			array (
				'email',
				'changeEmail',
				'on' => self::UPDATE . ',' . self::CREATE
			),
			array (
				'language',
				'changeLanguage',
				'on' => self::UPDATE . ',' . self::CREATE
			),
			array (
				'password_check',
				'changePassword',
				'on' => self::CREATE . ',' . self::CHANGE_PASSWORD
			),
			array (
				'email',
				'save'
			)
		);
	}

	public function attributeLabels() {
		return array (
			'username' => Yii::t('user', 'Username'),
			'password' => Yii::t('user', 'Password'),
			'password_check' => Yii::t('user', 'Check password'),
			'email' => Yii::t('user', 'Mail address'),
			'roles' => Yii::t('user', 'Roles'),
			'first_name' => Yii::t('user', 'First Name'),
			'last_name' => Yii::t('user', 'Last Name'),
			'phone_number' => Yii::t('user', 'Phone Number'),
			'initials' => Yii::t('user', 'Initials'),
			'simulation' => Yii::t('user', 'Simulation')
		);
	}

	public function changePassword($attribute, $params) {
		if ($this->password != $this->password_check) {
			$this->addError('password', Yii::t('user', 'Enterred passwords are differents.'));
		} else {
			$user = $this->getUser();
			if (!$this->hasErrors()) {
				$user->password = md5($this->password);
			}
		}
	}

	public function changeEmail($attribute, $params) {
		$user = $this->getUser();
		if (!$this->hasErrors()) {
			$user->email = $this->email;
		}
	}

	public function changeLanguage($attribute, $params) {
		$user = $this->getUser();
		if (!$this->hasErrors()) {
			$user->language = $this->language;
		}
	}

	protected function getUser() {
		if ($this->user == null) {
			$this->user = User::model()->findByAttributes(array (
				'username' => $this->username
			));
			if ($this->user == null && !$this->scenario == self::CREATE) {
				$this->addError('username', Yii::t('user', 'Unknown user {username}', array (
					'{username}' => $this->username
				)));
			}
			if ($this->user != null && $this->scenario == self::CREATE) {
				$this->addError('username', Yii::t('user', 'User {username} already exists', array (
					'{username}' => $this->username
				)));
			}
			if ($this->user == null && $this->scenario == self::CREATE) {
				$this->user = new User();
			}
		}
		$this->user->attributes = $this->attributes;
		
		return $this->user;
	}
	
	public function getRoles()
	{
		if ($this->roles === null && $this->username !== null)
		{
			$roles = Yii::app()->authManager->getAuthAssignments($this->username);
			$this->roles = array();
			foreach($roles as $role)
			{
				$this->roles[] = $role->itemName;
			}
		}
		
		return $this->roles;
	}
	
	public function setRoles($roles)
	{
		$this->roles = $roles;
	}

	public function save() {
		if (!$this->hasErrors())
		{
			$this->user->save();
			$currentRoles = array_merge(array(), $this->roles);
			$roles = Yii::app()->authManager->getAuthAssignments($this->username);

			// Revoke all removed roles
			foreach ($roles as $role)
			{
				$key = array_search($role, $currentRoles, true);
				if ($key === false)
				{
					// revoke the role
					Yii::app()->authManager->revoke($role->itemName, $this->username);
				} else {
					// Removing for current roles
					unset($currentRoles[$key]);
				}
			}
			
			// Assign all new roles
			foreach ($currentRoles as $role)
			{
					Yii::app()->authManager->assign($role, $this->username);
			}
		}
	}

}
?>
